import {inject, Injectable} from "@angular/core";
import {delay, Observable, of, tap} from "rxjs";
import {LocalStorageService} from "./local-storage.service";
import { User } from "../data/coommon/User ";

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  redirectUrl: string | null = null;
  public localStorageService: LocalStorageService = inject(LocalStorageService);

  constructor() {
  }

  login({username, pwd}: any): Observable<boolean> {
    console.log(username, pwd)
    return of(true).pipe(delay(1000), tap(() => {
      this.localStorageService.set('userName', username);
    }));
  }

  logout(): void {
    this.localStorageService.remove('userName');
  }

  isLogin(): boolean | undefined {
    let a = this.localStorageService.get('userName');
    console.log("a = " + a);
    return a != undefined;
  }

  isAdmin(username: String | null, id: number | null): boolean {
    console.log("username = ", username, " id = ", id);
    if (!username || !id) {

    }
    if (!(username?.includes('admin')) && id != undefined && id % 3 == 1) {
      console.log("需要管理员权限")
      return false
    }
    return true;
  }

  canMatch(currentUser: User): boolean {
    return true;
  }
}
